package com.cskaoyan.service;



import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.brand.BrandListBeanVo;
import com.cskaoyan.bean.brand.BrandListVo;
import com.cskaoyan.bean.market.*;

import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    BrandMapper brandMapper;


    //admin/brand/list?page=1&limit=20&sort=add_time&order=desc
    @Override
    public BrandList list(BaseParam baseParam, Integer id, String name) {
        PageUtil.start(baseParam);
        //
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        //
        if(id != null && !"".equals(id)){
            brandExample.createCriteria().andIdEqualTo(id);
        }
        if(name != null && "".equals(name)){
            brandExample.createCriteria().andNameEqualTo(name);
        }
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        int pages = brandPageInfo.getPages();
        long total = brandPageInfo.getTotal();
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        BrandList brandList = new BrandList();
        brandList.setTotal((int) total);
        brandList.setPages(pages);
        brandList.setPage(page);
        brandList.setLimit(limit);
        ArrayList<BrandListBean> brandListBeans = new ArrayList<>();
        for (Brand brand : brands) {
            if(brand.getDeleted() == false){
                BrandListBean brandListBean = new BrandListBean();
                brandListBean.setId(brand.getId());
                brandListBean.setName(brand.getName());
                brandListBean.setDesc(brand.getDesc());
                brandListBean.setPicUrl(brand.getPicUrl());
                brandListBean.setSortOrder(brand.getSortOrder());
                BigDecimal floorPrice = brand.getFloorPrice();
                brandListBean.setFloorPrice(floorPrice);
                brandListBean.setAddTime(brand.getAddTime());
                brandListBean.setUpdateTime(brand.getUpdateTime());
                brandListBean.setDeleted(brand.getDeleted());
                brandListBeans.add(brandListBean);
            }
        }
        brandList.setList(brandListBeans);
        return brandList;
    }

    @Override
    public void delete(Brand brand) {
        Brand brand1 = new Brand();
        brand1.setId(brand.getId());
        brand1.setDeleted(true);
        Date updateTime = new Date();
        brand1.setUpdateTime(updateTime);
        brandMapper.updateByPrimaryKeySelective(brand1);
    }

    @Override
    public Brand update(Brand brand) {
        Brand brand1 = new Brand();
        if(brand.getDesc() != null && !"".equals(brand.getDesc())){
            brand1.setDesc(brand.getDesc());
        }
        if (brand.getName() != null && !"".equals(brand.getName())) {
            brand1.setName(brand.getName());
        }
        if(brand.getFloorPrice() != null && !"".equals(brand.getFloorPrice())){
            brand1.setFloorPrice(brand.getFloorPrice());
        }
        if (brand.getPicUrl() != null && !"".equals(brand.getPicUrl())) {
            brand1.setPicUrl(brand.getPicUrl());
        }
        Date date = new Date();
        brand1.setUpdateTime(date);
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andIdEqualTo(brand.getId());
        brandMapper.updateByExampleSelective(brand1,brandExample);
        brand.setUpdateTime(date);
        return brand;
    }


    @Override
    public BrandCreateVo create(BrandCreateBo brandCreateBo) {
        String name = brandCreateBo.getName();
        BrandExample brandExample1 = new BrandExample();
        brandExample1.createCriteria().andNameEqualTo(name);
        List<Brand> brands1 = brandMapper.selectByExample(brandExample1);
        if(brands1.size() == 1){
            return null;
        }
        if("".equals(brandCreateBo.getPicUrl()) || brandCreateBo.getFloorPrice() == null){
            BrandCreateVo brandCreateVo = new BrandCreateVo();
            brandCreateVo.setDesc("a");
            return brandCreateVo;
        }
        Brand brand1 = new Brand();
        brand1.setName(brandCreateBo.getName());
        brand1.setDesc(brandCreateBo.getDesc());
        brand1.setPicUrl(brandCreateBo.getPicUrl());
        brand1.setFloorPrice(brandCreateBo.getFloorPrice());
        Byte sortOrder = 50;
        brand1.setSortOrder(sortOrder);
        brand1.setDeleted(false);
        Date date = new Date();
        brand1.setAddTime(date);
        brand1.setUpdateTime(date);
        brandMapper.insertSelective(brand1);
        BrandCreateVo brandCreateVo = new BrandCreateVo();
        Integer id = brand1.getId();
        brandCreateVo.setId(id);
        brandCreateVo.setName(brand1.getName());
        brandCreateVo.setDesc(brand1.getDesc());
        brandCreateVo.setPicUrl(brand1.getPicUrl());
        brandCreateVo.setFloorPrice(brand1.getFloorPrice());
        brandCreateVo.setAddTime(brand1.getAddTime());
        brandCreateVo.setUpdateTime(brand1.getUpdateTime());
        return brandCreateVo;

    }

    @Override
    public BrandListVo cartlist(BaseParam baseParam) {
        PageUtil.start(baseParam);
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        int pages = brandPageInfo.getPages();
        Integer limit = baseParam.getLimit();
        int pageNum = brandPageInfo.getPageNum();
        ArrayList<BrandListBeanVo> brandListBeanVos = new ArrayList<>();
        for (Brand brand : brands) {
            BrandListBeanVo brandListBeanVo = new BrandListBeanVo(brand.getId(),brand.getName(),brand.getDesc(),brand.getPicUrl(),brand.getFloorPrice().intValue());
            brandListBeanVos.add(brandListBeanVo);
        }
        BrandListVo brandListVo = new BrandListVo();
        brandListVo.setLimit(limit);
        brandListVo.setList(brandListBeanVos);
        brandListVo.setPage(pageNum);
        brandListVo.setPages(pages);
        brandListVo.setTotal((int) total);
        return brandListVo;
    }

    @Override
    public Brand cartDetail(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }
}
